﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace OpenSource.Controls
{
    public class InputFormTemplateCell : InputFormCell
    {


        private ITemplate itemTemplate;

        /// <devdoc> 
        ///    Initializes a new instance of the <see cref='System.Web.UI.WebControls.TemplateField'/> class.
        /// </devdoc> 
        public InputFormTemplateCell()
        {
        }




        /// <devdoc> 
        /// <para> Specifies the <see cref='System.Web.UI.ITemplate' qualify='true'/> that defines how items are rendered. </para>
        /// </devdoc> 
        [Browsable(false),
        DefaultValue(null),
        Description("SR.TemplateField_ItemTemplate"),
        PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(IDataItemContainer), BindingDirection.TwoWay)]
        public virtual ITemplate ItemTemplate
        {
            get { return itemTemplate; }
            set
            {
                itemTemplate = value;
                OnFieldChanged();
            }
        }

        protected override void CopyProperties(InputFormCell newField)
        {

            ((InputFormTemplateCell)newField).ItemTemplate = ItemTemplate;
            base.CopyProperties(newField);
        }

        protected override InputFormCell CreateField()
        {
            return new InputFormTemplateCell();
        }



        /// <internalonly/> 
        /// <devdoc>
        /// </devdoc> 
        public override void InitializeCell(InputFormCellField field)
        // DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(field); // cell, cellType, rowState, rowIndex);

            ITemplate contentTemplate = this.itemTemplate;// null;
            if (contentTemplate != null)
            {
                // The base class might have added a control or some text for some cases
                // such as header text which need to be removed before
                // the corresponding template is used.
                // Note that setting text also has the effect of clearing out any controls. 
                contentTemplate.InstantiateIn(field);
            }
        }

        /// <devdoc> 
        /// <para>Override with an empty body if the field's controls all support callback.
        ///  Otherwise, override and throw a useful error message about why the field can't support callbacks.</para> 
        /// </devdoc>
        public override void ValidateSupportsCallback()
        {

        }
    }
}
